package com.njupt.Array;

/**
 * 69. x 的平方根
 */
public class MySqrt {

    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int mid = -1;
        int ans = -1;
        while(left<=right){
            mid = (left + right) / 2;

            if((long)mid * mid <= x){
                ans = mid;
                left = mid + 1;
            }else {
                right = mid - 1;
            }
        }
        return ans;
    }

    public int mySqrt1(int x){
        long result =0;
        int temp =0;
        while(result <= x){
            if(result == x){
                return temp;
            }
            temp++;
            result = temp * temp;
        }
        return temp -1;
    }

    public int mySqrt2(int x) {
        int l = 0, r = x, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if ((long) mid * mid <= x) {
                ans = mid;
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        MySqrt test = new MySqrt();
        System.out.println(test.mySqrt1(2147483647));
    }
}
